/*
Copyright [2023] [fuwen_soft]
*/
package vip.xiaonuo.modular.xyethniccate.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import vip.xiaonuo.core.consts.CommonConstant;
import vip.xiaonuo.core.enums.CommonStatusEnum;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.util.PoiUtil;
import vip.xiaonuo.modular.xyethniccate.entity.XyEthnicCate;
import vip.xiaonuo.modular.xyethniccate.enums.XyEthnicCateExceptionEnum;
import vip.xiaonuo.modular.xyethniccate.mapper.XyEthnicCateMapper;
import vip.xiaonuo.modular.xyethniccate.param.XyEthnicCateParam;
import vip.xiaonuo.modular.xyethniccate.service.XyEthnicCateService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

/**
 * 族员的 关系排行service接口实现类
 *
 * @author yanwei
 * @date 2023-05-05 15:44:28
 */
@Service
public class XyEthnicCateServiceImpl extends ServiceImpl<XyEthnicCateMapper, XyEthnicCate> implements XyEthnicCateService {

    @Override
    public PageResult<XyEthnicCate> page(XyEthnicCateParam xyEthnicCateParam) {
        QueryWrapper<XyEthnicCate> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(xyEthnicCateParam)) {

            // 根据名称 查询
            if (ObjectUtil.isNotEmpty(xyEthnicCateParam.getJoinName())) {
                queryWrapper.lambda().eq(XyEthnicCate::getJoinName, xyEthnicCateParam.getJoinName());
            }
            // 根据1=(母,妻),2=(兄妹),3=(儿排行),4=(女排行) 查询
            if (ObjectUtil.isNotEmpty(xyEthnicCateParam.getTypeFlag())) {
                queryWrapper.lambda().eq(XyEthnicCate::getTypeFlag, xyEthnicCateParam.getTypeFlag());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<XyEthnicCate> list(XyEthnicCateParam xyEthnicCateParam) {
        return this.list();
    }

    @Override
    public void add(XyEthnicCateParam xyEthnicCateParam) {
        XyEthnicCate xyEthnicCate = new XyEthnicCate();
        BeanUtil.copyProperties(xyEthnicCateParam, xyEthnicCate);
        this.save(xyEthnicCate);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<XyEthnicCateParam> xyEthnicCateParamList) {
        xyEthnicCateParamList.forEach(xyEthnicCateParam -> {
            this.removeById(xyEthnicCateParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(XyEthnicCateParam xyEthnicCateParam) {
        XyEthnicCate xyEthnicCate = this.queryXyEthnicCate(xyEthnicCateParam);
        BeanUtil.copyProperties(xyEthnicCateParam, xyEthnicCate);
        this.updateById(xyEthnicCate);
    }

    @Override
    public XyEthnicCate detail(XyEthnicCateParam xyEthnicCateParam) {
        return this.queryXyEthnicCate(xyEthnicCateParam);
    }

    /**
     * 获取族员的 关系排行
     *
     * @author yanwei
     * @date 2023-05-05 15:44:28
     */
    private XyEthnicCate queryXyEthnicCate(XyEthnicCateParam xyEthnicCateParam) {
        XyEthnicCate xyEthnicCate = this.getById(xyEthnicCateParam.getId());
        if (ObjectUtil.isNull(xyEthnicCate)) {
            throw new ServiceException(XyEthnicCateExceptionEnum.NOT_EXIST);
        }
        return xyEthnicCate;
    }

    @Override
    public void export(XyEthnicCateParam xyEthnicCateParam) {
        List<XyEthnicCate> list = this.list(xyEthnicCateParam);
        PoiUtil.exportExcelWithStream("SnowyXyEthnicCate.xls", XyEthnicCate.class, list);
    }


}
